package sgi.client;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JDesktopPane;
import java.awt.BorderLayout;
import java.io.IOException;

public class SGISubscribeUI extends SGIClientFrame {
	private SGISubscribeController SubscribeController;
	private JComboBox comboBox;
	private	JDesktopPane desktopPane; 
	private JPanel Acceptbutton;
	private JButton btnSubmit;
	private JButton btnAccept;
	private JButton btnReturn;
	private JComponent currentPanel;
	private JPanel defaultpanel;
	public SGISubscribeUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,100,571,315);
		desktopPane = new JDesktopPane();
		getContentPane().add(desktopPane, null);
		setResizable(false);
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 571, 52);
		panel.setLayout(null);
		desktopPane.add(panel);

		defaultpanel = new JPanel();
		defaultpanel.setBounds(0, 50, 571, 166);
		defaultpanel.setLayout(null);
		desktopPane.add(defaultpanel);
		currentPanel=defaultpanel;
		
		comboBox = new JComboBox();
		panel.add(comboBox);
		comboBox.setBounds(10, 11, 192, 33);
		comboBox.addItem("Select Subscription type...");
		comboBox.addItem("Data Layer Subscription");
		comboBox.addItem("Location Time Subscription");
		comboBox.addItem("Image Pack Subscription");
		comboBox.addActionListener (new ActionListener () {
		    public void actionPerformed(ActionEvent e) {
		    	switchPanel();
		    }
		});
		
		Acceptbutton= new JPanel();
		Acceptbutton.setBounds(0, 216, 571, 77);
		desktopPane.add(Acceptbutton);
		Acceptbutton.setLayout(null);
		
		btnAccept = new JButton("Accept");
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(currentPanel instanceof DataLayerSubscriptionPanel)
				{
					
					if(((DataLayerSubscriptionPanel)currentPanel).getchckbxCommerce()== true)
					{
						if(((DataLayerSubscriptionPanel)currentPanel).getChckbxInfrastracture()== true)
						{
							try {
								SubscribeController.subscribeDataLayers(SGIUserAccountMenuController.getUser().getUsername(),1,1);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						else
						{
							try {
								SubscribeController.subscribeDataLayers(SGIUserAccountMenuController.getUser().getUsername(),1,0);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}							
						}
					}
					else if(((DataLayerSubscriptionPanel)currentPanel).getChckbxInfrastracture()== true)
					{
						try {
							SubscribeController.subscribeDataLayers(SGIUserAccountMenuController.getUser().getUsername(),0,1);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else
					{
						JOptionPane.showMessageDialog(SGISubscribeController.getUI(),"Choose Data Layer Subscription");
					}
				}
				else if(currentPanel instanceof DateTimeSubscriptionPanel)
				{
					if(((DateTimeSubscriptionPanel)currentPanel).getlocation().equals(""))
						JOptionPane.showMessageDialog(SGISubscribeController.getUI(),"You must fill Location field.");
					try {
						SubscribeController.subscribeDateTime(SGIUserAccountMenuController.getUser().getUsername(), ((DateTimeSubscriptionPanel)currentPanel).getlocation());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				else if(currentPanel instanceof ImagePackSubscriptionPanel)
				{

					try {
						SubscribeController.subscribeImagePack(SGIUserAccountMenuController.getUser().getUsername());
					} catch (IOException e) {
						e.printStackTrace();
					}					
				}
			}
		});
		btnAccept.setBounds(397, 28, 164, 38);
		Acceptbutton.add(btnAccept);
		
		
		btnReturn = new JButton("Return");
		btnReturn.setBounds(10, 28, 164, 38);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGISubscribeController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		Acceptbutton.add(btnReturn);	
	}
	
	public void switchPanel()
	{
		 switch(comboBox.getSelectedIndex())
	        {
	        		case 0:
	        		{
	        			currentPanel.setVisible(false);
	        			JPanel defaultPanel = new JPanel();
	        			currentPanel=defaultPanel;
	        			desktopPane.add(defaultPanel);
	        			defaultPanel.setBounds(0, 50, 571, 166);
	        			currentPanel.setVisible(true);
	        			repaint();
	        		}break;
	        		case 1:
	        		{
	        			currentPanel.setVisible(false);
	        			DataLayerSubscriptionPanel datapanel = new DataLayerSubscriptionPanel();
	        			currentPanel = datapanel;
	        			desktopPane.add(currentPanel);
	        			datapanel.setBounds(0, 50, 571, 166);
	        			currentPanel.setVisible(true);
	        			repaint();
	        		}break;
	        		
	        		case 2:
	        		{
	        			currentPanel.setVisible(false);
	        			DateTimeSubscriptionPanel datetimePanel = new DateTimeSubscriptionPanel();
	        			currentPanel = datetimePanel;
	        			desktopPane.add(currentPanel);
	        			datetimePanel.setBounds(0, 50, 571, 166);
	        			currentPanel.setVisible(true);
	        			repaint();
	        		}break;
	        		case 3:
	        		{
	        			currentPanel.setVisible(false);
	        			ImagePackSubscriptionPanel imagepackPanel = new ImagePackSubscriptionPanel();
	        			currentPanel = imagepackPanel;
	        			desktopPane.add(currentPanel);
	        			imagepackPanel.setBounds(0, 50, 571, 166);
	        			currentPanel.setVisible(true);
	        			repaint();
	        		}break;
	        }
	}

	public void notify(String str)
	{
		JOptionPane.showMessageDialog(this,str);
	}
	
	public SGISubscribeController getSubscribeController() {
		return SubscribeController;
	}

	public void setSubscribeController(SGISubscribeController SubscribeController) {
		this.SubscribeController = SubscribeController;
	}
}
